//
//  Copyright (C) 2011 Ray Wang(fangshi168775@hotmail.com).  All Rights Reserved.
//
//  This is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
//
//  This software is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this software; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
//  USA.
//
package ray.pcvnccontroler;

import java.io.IOException;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuInflater;
import android.view.MenuItem.OnMenuItemClickListener;
import android.app.AlertDialog.Builder;

public class ShortCutDialog extends Dialog
{
	private KeySender keySnd;
	private Context context;
	
	
	public ShortCutDialog(Context context, RfbProto rfb)
	{
		super(context, android.R.style.Theme);
		setOwnerActivity((Activity)context);
		this.context = context;
		keySnd = new KeySender(rfb);
		
	}
	
	protected void onCreate(Bundle savedInstanceState) 
	{
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.shortcut);
		setTitle(R.string.shortcut);
		
		createClickListerner();
	}
	
	private void AskToShutdown()
	{
		Builder builder = new Builder(context);
		builder.setMessage(R.string.ask_to_shutdown)
		       .setCancelable(false)
		       .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		        	   try {
						keySnd.SendShortcut(KeySender.SHUTDOWN_SHORTCUT);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
		           }
		       })
		       .setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		                dialog.cancel();
		           }
		       });
		AlertDialog alert = builder.create();
		alert.show();
	}
	
	public class BtnOnClickListener implements View.OnClickListener
	{
		int shortcut;
		BtnOnClickListener(int shortcut)
		{
			this.shortcut = shortcut;
		}
		public void onClick(View v) 
		{
			try
			{
				if(shortcut == KeySender.SHUTDOWN_SHORTCUT)
				{
					AskToShutdown();
				}
				else
				{
					keySnd.SendShortcut(shortcut);
				}
			}
			catch (Exception e)
			{
				int duration = Toast.LENGTH_SHORT;
				Toast toast = Toast.makeText(context, R.string.ctrl_error, duration);
				toast.show();
			}
		}
	}
	
	private void createClickListerner()
	{
		final int dlg_id[] = 
		{
				R.id.btn_start, R.id.btn_desktop, R.id.btn_search, 
				R.id.btn_my_computer, R.id.btn_ie, R.id.btn_focus_change,
				R.id.btn_lock_window, R.id.btn_window_change, R.id.btn_cancel,
				R.id.btn_zoom, R.id.btn_close_page, R.id.btn_close_window,
				R.id.btn_mute, R.id.btn_run,
				R.id.btn_right, R.id.btn_program_menu, R.id.btn_shutdown,
				R.id.btn_back, R.id.btn_forward
		};
		
		final int shortcut_id[] = 
		{
				KeySender.START_SHORTCUT, KeySender.DESKTOP_SHORTCUT, KeySender.SEARCH_SHORTCUT,
				KeySender.MY_COMPUTER_SHORTCUT, KeySender.IE_SHORTCUT, KeySender.FOCUS_CHANGE_SHROTCUT,
				KeySender.LOCK_WINDOW_SHORTCUT, KeySender.WINDOW_CHANGE_SHORTCUT, KeySender.CANCEL_SHORTCUT,
				KeySender.ZOOM_SHORTCUT, KeySender.CLOSE_PAGE_SHORTCUT, KeySender.CLOSE_WINDOW_SHORTCUT,
				KeySender.MUTE_SHORTCUT, KeySender.RUN_SHORTCUT,
				KeySender.RIGHT_KEY_SHORTCUT, KeySender.PROGRAM_MENU_SHORTCUT,KeySender.SHUTDOWN_SHORTCUT,
				KeySender.BACK_SHORTCUT, KeySender.FORWARD_SHORTCUT
		};
		
		int size = dlg_id.length;
		for(int i=0; i<size; i++)
		{
			Button btn;
			btn = (Button) findViewById(dlg_id[i]);
			btn.setOnClickListener(new BtnOnClickListener(shortcut_id[i]));
		}
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		boolean result = true;
		try
		{
			switch(keyCode)
			{
			case KeyEvent.KEYCODE_DPAD_LEFT:
			case KeyEvent.KEYCODE_DPAD_RIGHT:
			case KeyEvent.KEYCODE_DPAD_UP:
			case KeyEvent.KEYCODE_DPAD_DOWN:
			case KeyEvent.KEYCODE_DPAD_CENTER:
				result = keySnd.SendKeyEvent(keyCode, true);
				break;
			case KeyEvent.KEYCODE_BACK:
				cancel();
				break;
			case KeyEvent.KEYCODE_VOLUME_UP:
				keySnd.SendShortcut(KeySender.VOL_UP_START_SHORTCUT);
				break;
			case KeyEvent.KEYCODE_VOLUME_DOWN:
				keySnd.SendShortcut(KeySender.VOL_DOWN_START_SHORTCUT);
				break;
			default:
				result = false;
			}
		}
		catch (Exception e)
		{
			int duration = Toast.LENGTH_SHORT;
			Toast toast = Toast.makeText(context, R.string.ctrl_error, duration);
			toast.show();
		}
		return result;
	}
	
	public boolean onKeyUp(int keyCode, KeyEvent event)
	{
		boolean result = true;
		try
		{
			switch(keyCode)
			{
			case KeyEvent.KEYCODE_DPAD_LEFT:
			case KeyEvent.KEYCODE_DPAD_RIGHT:
			case KeyEvent.KEYCODE_DPAD_UP:
			case KeyEvent.KEYCODE_DPAD_DOWN:
			case KeyEvent.KEYCODE_DPAD_CENTER:
				result = keySnd.SendKeyEvent(keyCode, false);
				break;
			case KeyEvent.KEYCODE_VOLUME_UP:
				keySnd.SendShortcut(KeySender.VOL_UP_END_SHORTCUT);
				break;
			case KeyEvent.KEYCODE_VOLUME_DOWN:
				keySnd.SendShortcut(KeySender.VOL_DOWN_END_SHORTCUT);
				break;
			default:
				result = false;
			}
		}
		catch (Exception e)
		{
			int duration = Toast.LENGTH_SHORT;
			Toast toast = Toast.makeText(context, R.string.ctrl_error, duration);
			toast.show();
		}
		return result;
	}
	
	
	
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
	    MenuInflater inflater = getOwnerActivity().getMenuInflater();
	    inflater.inflate(R.menu.shortcut, menu);
	    menu.findItem(R.id.menu_input).setOnMenuItemClickListener(new OnMenuItemClickListener() {

			@Override
			public boolean onMenuItemClick(MenuItem item) {
				getOwnerActivity().showDialog(R.layout.input);
				return true;
			}
		});
	    
	    return true;
	}
	
    
}
